package Symmetric

import "../goEncrypt"
import (
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

type DES3 struct {
}

//字节加密
func (d *DES3) Encrypt(datastr []byte, password []byte) []byte {

	length := len(password)
	if length < 24 {
		for i := 0; i <= 24-1-length; i++ {
			password = append(password, 0)
		}
	} else if length > 24 {
		password = password[:24]
	}

	cryptText, err := goEncrypt.TripleDesEncrypt(datastr, password)
	if err != nil {
		fmt.Println(err)
		return []byte{}
	}
	return cryptText
}
func (d *DES3) Decrypt(datastr []byte, password []byte) []byte {
	length := len(password)
	if length < 24 {
		for i := 0; i <= 24-1-length; i++ {
			password = append(password, 0)
		}
	} else if length > 24 {
		password = password[:24]
	}
	//fmt.Println(len(password))
	newplaintext, err := goEncrypt.TripleDesDecrypt(datastr, password)
	if err != nil {
		fmt.Println(err)
		return []byte{}
	}
	return newplaintext

}
func (d *DES3) EncryptString(datastr string, password string) string {
	length := len(password)
	passbyte := make([]byte, 24, 24)
	if length < 24 {
		for i := 0; i < length; i++ {
			passbyte[i] = password[i]
		}
	} else if length > 24 {
		for i := 0; i < 24; i++ {
			passbyte[i] = password[i]
		}
	} else {
		for i := 0; i < 24; i++ {
			passbyte[i] = password[i]
		}
	}
	cryptText, err := goEncrypt.TripleDesEncrypt([]byte(datastr), passbyte)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return hex.EncodeToString(cryptText)
}
func (d *DES3) DecryptString(datastr string, password string) string {
	length := len(password)
	passbyte := make([]byte, 24, 24)
	if length < 24 {
		for i := 0; i < length; i++ {
			passbyte[i] = password[i]
		}
	} else if length > 24 {
		for i := 0; i < 24; i++ {
			passbyte[i] = password[i]
		}
	} else {
		for i := 0; i < 24; i++ {
			passbyte[i] = password[i]
		}
	}
	databyte, _ := hex.DecodeString(datastr)
	newplaintext, err := goEncrypt.TripleDesDecrypt(databyte, passbyte)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return string(newplaintext)

}

func makepath(path string, isen bool) string {
	filelist := strings.Split(path, "\\")
	if len(filelist) < 1 {
		return ""
	}
	lastpath := ""
	for i := 0; i < len(filelist)-1; i++ {
		lastpath += filelist[i] + "\\"
	}
	if isen {
		lastpath += "en_"
	} else {
		lastpath += "de_"
	}
	lastpath += filelist[len(filelist)-1]
	//fmt.Println(lastpath)
	return lastpath
}

func (d *DES3) EncryptFilePath(filepath string, password string) string {
	file, err := os.Open(filepath)
	if err != nil {
		return ""
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	crptdata := d.Encrypt(data, []byte(password)) //加密以后的数据
	//fmt.Println(len(crptdata))
	savepath := makepath(filepath, true)
	savefile, err := os.Create(savepath)
	if err != nil {
		return ""
	}
	defer savefile.Close()
	savefile.Write(crptdata)
	//写入
	//返回路径

	return savepath
}
func (d *DES3) DecryptFilePath(filepath string, password string) string {
	file, err := os.Open(filepath)
	if err != nil {
		return ""
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	crptdata := d.Decrypt(data, []byte(password)) //解密以后的数据
	//fmt.Println(len(crptdata))
	savepath := makepath(filepath, false)
	savefile, err := os.Create(savepath)
	if err != nil {
		return ""
	}
	defer savefile.Close()
	savefile.Write(crptdata)
	//写入
	//返回路径

	return savepath
}
